<template>
  <div class="my-counter">
    <button type="button" class="btn btn-light" @click="reduce">-</button>
    <input
      type="number"
      class="form-control inp"
      :value="count"
      @change="change"
    />
    <button type="button" class="btn btn-light" @click="increase">+</button>
  </div>
</template>

<script>
export default {
  methods: {
    reduce() {
      this.bus.$emit("reduce", this.count, this.id);
    },
    increase() {
      this.bus.$emit("add", this.count, this.id);
    },
    // 用事件对象获取input的value值
    change(e) {
      // | 0 用来取整
      let value = +e.target.value | 0;
      // console.log(value);
      // 因为输入两次小于0的数，判断小于0就会等于1，第二次还是小于0，等于1  数据没有改变，虚拟Dom不会更新，所以需要强制更新组件
      this.$forceUpdate();
      this.bus.$emit("change", value, this.id);
    },
  },
  props: {
    count: {
      type: Number,
      default: 1,
    },
    id: {
      type: Number,
      required: true,
    },
  },
};
</script>

<style lang="less" scoped>
.my-counter {
  display: flex;
  .inp {
    width: 45px;
    text-align: center;
    margin: 0 10px;
  }
}
</style>